//
//  WDCalendarScrollView.m
//  MyDemos
//
//  Created by iMac on 2018/3/6.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "WDCalendarScrollView.h"
#import "WDCalendarMonthView.h"
#import "WDCalendarDateHandler.h"
#import "WDCalendarDayModel.h"



@interface WDCalendarScrollView() <UIScrollViewDelegate>

@property (nonatomic, strong) WDCalendarMonthView *lastMonth;
@property (nonatomic, strong) WDCalendarMonthView *currentMonth;
@property (nonatomic, strong) WDCalendarMonthView *nextMonth;
@property (nonatomic, strong) NSArray *calendarModelsGroup;

@end

@implementation WDCalendarScrollView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupInit];
        self.backgroundColor = [UIColor grayColor];
    }
    return self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    WDCalendarDateHandler *handler = [WDCalendarDateHandler sharedInstance];
    NSDate *currentDate = [NSDate date];
    if (self.contentOffset.x == 0) {
        currentDate = [handler dateForLastMonth:currentDate];
    } else if (self.contentOffset.x == SCREEN_WIDTH * 2.f) {
        currentDate = [handler dateForNextMonth:currentDate];
    } else {
        return;
    }
    self.calendarModelsGroup = [handler dataForCalendar:currentDate];
    [self updateFrames:currentDate];
    self.contentOffset = CGPointMake(0, 0);
}

- (void)setupInit {
    CGFloat width = SCREEN_WIDTH;
    CGFloat itemWidth = width / 7.f;
    WDCalendarDateHandler *handler = [WDCalendarDateHandler sharedInstance];
    CGRect lastMonthRect = CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH, itemWidth * [handler numsOfRows:[handler dateForLastMonth:[NSDate date]]]);
    self.lastMonth = [[WDCalendarMonthView alloc] initWithFrame:lastMonthRect];
    
    CGRect currentMonthRect = CGRectMake(0, 0, SCREEN_WIDTH, itemWidth * [handler numsOfRows:[NSDate date]]);
    self.currentMonth = [[WDCalendarMonthView alloc] initWithFrame:currentMonthRect];
    
    CGRect nextMonthRect = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, itemWidth * [handler numsOfRows:[handler dateForNextMonth:[NSDate date]]]);
    self.nextMonth = [[WDCalendarMonthView alloc] initWithFrame:nextMonthRect];
    
    [self addSubview:self.lastMonth];
    [self addSubview:self.currentMonth];
    [self addSubview:self.nextMonth];
    
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, currentMonthRect.size.height);
    self.contentSize = CGSizeMake(SCREEN_WIDTH * 3.f, currentMonthRect.size.height);
    self.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    self.delegate = self;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.pagingEnabled = YES;
    self.bounces = NO;
    
    self.calendarModelsGroup = [handler dataForCalendar:[NSDate date]];
    
}

- (void)updateFrames:(NSDate *)currentDate {
    CGFloat width = SCREEN_WIDTH;
    CGFloat itemWidth = width / 7.f;
    WDCalendarDateHandler *handler = [WDCalendarDateHandler sharedInstance];
    CGRect lastMonthRect = CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH, itemWidth * [handler numsOfRows:[handler dateForLastMonth:currentDate]]);
    CGRect currentMonthRect = CGRectMake(0, 0, SCREEN_WIDTH, itemWidth * [handler numsOfRows:currentDate]);
    CGRect nextMonthRect = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, itemWidth * [handler numsOfRows:[handler dateForNextMonth:currentDate]]);
    self.lastMonth.frame = lastMonthRect;
    self.currentMonth.frame = currentMonthRect;
    self.nextMonth.frame = nextMonthRect;
    self.frame = currentMonthRect;
}
- (void)setCalendarModelsGroup:(NSArray *)calendarModelsGroup {
    _calendarModelsGroup = calendarModelsGroup;
    self.lastMonth.datas = calendarModelsGroup[0];
    self.currentMonth.datas = calendarModelsGroup[1];
    self.nextMonth.datas = calendarModelsGroup[2];
    WDCalendarDayModel *model = calendarModelsGroup[1][0];
    [self updateFrames:model.date];
}
@end
